﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Service;
using MODEL;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using Microsoft.Practices.ServiceLocation;
using GalaSoft.MvvmLight.Views;
using Common;
using System.ComponentModel;
using GalaSoft.MvvmLight.Ioc;

namespace ViewModel.StationManager
{
    public class StationElementEditViewModel : ViewModelBase
    {
        public ViewModelBase ParentViewModel { get; set; }

        public ICommand SaveCommand { get; set; }

        private ElementInfo m_ElementInfo = new ElementInfo();
        public ElementInfo ElementInfo
        {
            get { return this.m_ElementInfo; }
            set { this.m_ElementInfo = value; base.RaisePropertyChanged(() => this.ElementInfo); }
        }

        private List<string> m_StationTypeList = new List<string>();
        public List<string> StationTypeList
        {
            get { return this.m_StationTypeList; }
            set { this.m_StationTypeList = value; base.RaisePropertyChanged(() => this.StationTypeList); }
        }
        private static readonly string NonSelectedStationType = "未选择站点类型";
        private string m_CurrentStationType;
        public string CurrentStationType
        {
            get { return this.m_CurrentStationType; }
            set { this.m_CurrentStationType = value; base.RaisePropertyChanged(() => CurrentStationType); }
        }

        private IElementService m_ElementService;

        public StationElementEditViewModel(IElementService elementService)
        {
            this.m_ElementService = elementService;

            this.SaveCommand = new RelayCommand(AddElement);
            RefreshData();
        }

        public void RefreshData()
        {
            List<string> stationTypeList = new List<string> { NonSelectedStationType };
            stationTypeList.AddRange(EnumHelper.GetEnumDescriptionList(typeof(StationTypeEnum)));
            this.StationTypeList = stationTypeList;
            this.CurrentStationType = NonSelectedStationType;
        }

        public async void AddElement()
        {
            if (string.IsNullOrWhiteSpace(ElementInfo.ElementName))
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("要素名称不能为空", "校验失败");
                return;
            }

            if (string.IsNullOrWhiteSpace(ElementInfo.ElementCode))
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("要素编码不能为空", "校验失败");
                return;
            }

            if (string.IsNullOrEmpty(this.CurrentStationType) || this.CurrentStationType == NonSelectedStationType)
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("必须选择一个站点类型", "校验失败");
                return;
            }

            this.ElementInfo.StationType = this.CurrentStationType;
            try
            {
                Task task = this.m_ElementService.InsertElementInfoAsync(this.ElementInfo);
                task.Wait();
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("保存成功", "校验失败");

                this.MessengerInstance.Send<object>(null, "StationElement_CloseEditForm");
                (this.ParentViewModel as StationElementViewModel)?.QueryElements();
            }
            catch (Exception ex)
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowError("保存失败", "错误", "确定", null);
                Log.WriteException(ex);
            }
        }
    }
}
